/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;

using DotNetNuke.Entities.Modules;
using DotNetNuke.UI.WebControls;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Modules.Articles.Common;
using DotNetNuke.ModuleToolkit.Localization;

#endregion

namespace DotNetNuke.Modules.Articles.UI.Editors
{
    /// -----------------------------------------------------------------------------
    /// Namespace:  DotNetNuke.Modules.Articles.UI.Editors
    /// Class:      SkinEditControl
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// An Edit Control to Edit skins
    /// </summary>
    /// <history>
    /// 	[cnurse]	07/25/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class SkinEditControl : TextEditControl
    {

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the Folder where the Skins are located
        /// </summary>
        /// <value>A String</value>
        /// <history>
        ///     [anurse]  12/22/2005  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public string SkinsFolder
        {
            get
            {
                return String.Format("{0}/{1}", this.TemplateSourceDirectory, Constants.FOLDER_Skins);
            }
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the ModuleId of the parent Module
        /// </summary>
        /// <history>
        /// 	[cnurse]	04/21/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        private string GetSkinsFolder()
        {
            string _Folder = Null.NullString;
            System.Web.UI.Control myParent = this.Parent;
            while (myParent != null)
            {
                if (myParent is PortalModuleBase)
                {
                    _Folder = String.Format("{0}/{1}", ((PortalModuleBase)myParent).TemplateSourceDirectory, Constants.FOLDER_Skins);
                    break;
                }
                myParent = myParent.Parent;
            }

            return _Folder;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Render renders the control in edit mode
        /// </summary>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	04/20/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderEditMode(System.Web.UI.HtmlTextWriter writer)
        {
            //Get the Skins
            DirectoryInfo skinsDirectory = new DirectoryInfo(this.Context.Server.MapPath(GetSkinsFolder()));

            //Render the Select Tag
            ControlStyle.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            writer.RenderBeginTag(HtmlTextWriterTag.Select);

            //Render the "None Specifed" Option
            string notSpecifiedText = Localization.LocalizeString(this, Constants.TEXT_NotSpecified);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
            writer.RenderBeginTag(HtmlTextWriterTag.Option);
            writer.Write(notSpecifiedText);
            writer.RenderEndTag();

            foreach (DirectoryInfo skinDir in skinsDirectory.GetDirectories())
            {
                string skin = skinDir.Name;
                if (skinDir.Name.Equals(Constants.SKIN_Default))
                {
                    skin = Constants.TEXT_Default;
                }

                //Add the Value Attribute
                writer.AddAttribute(HtmlTextWriterAttribute.Value, skin);

                if (skinDir.Name == StringValue)
                    //Add the Selected Attribute
                    writer.AddAttribute(HtmlTextWriterAttribute.Selected, "selected");

                //Render Option Tag
                writer.RenderBeginTag(HtmlTextWriterTag.Option);
                writer.Write(skin);
                writer.RenderEndTag();
            }

            //Close Select Tag
            writer.RenderEndTag();
        }

    }

}
